template.defaults.imports.splitImages = function (imgsStr) {
    var imgs = imgsStr.split(',');
    template.defaults.imports.imgs = imgs;
};

template.defaults.imports.TimestampToTime = function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes();
    return Y + M + D + h + m ;
};

//下拉刷新
$("#tab1").pullToRefresh().on("pull-to-refresh", function () {
    $('#list-content').html('');
    resetCondition();
    queryList(function () {
        $("#tab1").pullToRefreshDone(); // 重置下拉刷新
    });
    return false;
});

//上拉加载更多
$('#tab1').infinite().on("infinite", function () {
    if (hasNextPage) {
        pageNo++;
        queryList(function () {}, true);
    }
});

//复位所有参数
/* global cate1 resetCate cate2 attrs*/
function resetCondition() {
    pageNo = 1;
}

var loading = false; //状态标记
var hasNextPage = false;
var pageNo = 1;
queryList();
function queryList(callback, isMore) {
    if (loading) return;
    loading = true;
    
    $.post('/antique/api/mallPosts/manage/query_list', {
        start: (pageNo - 1) * 20,
        limit: 20,
        token:getCookie('token')
    }, function (result) {
        $('#list-content').append(template('list', {
            list:result.data.list,
            avater:getCookie('photo'),
            name:getCookie('name')
        }));
        hasNextPage = result.data.hasNextPage;
        if (!result.data.hasNextPage) {
            $('#loadmore').hide();
            $('#nomore').show();
        } else {
            $('#loadmore').show();
            $('#nomore').hide();
        }
        if (callback) callback();
        loading = false;
    });
}

//下拉刷新
$("#tab2").pullToRefresh().on("pull-to-refresh", function () {
    $("#list-content2").html("");
    resetCondition2();
    queryList2(function () {
        $("#tab2").pullToRefreshDone(); // 重置下拉刷新
    });
    return false;
});

//上拉加载更多
$('#tab2').infinite().on("infinite", function () {
    if (hasNextPage2) {
        pageNo++;
        queryList2(function () {}, true);
    }
});

//复位所有参数
/* global cate1 resetCate cate2 attrs*/
function resetCondition2() {
    pageNo = 1;
}

var loading2 = false; //状态标记
var hasNextPage2 = false;
var pageNo2 = 1;
queryList2();
//查询我回复的帖子信息列表
function queryList2(callback, isMore) {
    if (loading2) return;
    loading2 = true;
    
    $.post('/antique/api/mallPosts/join/query_list', {
        start: (pageNo - 1) * 20,
        limit: 20,
        token:getCookie('token')
    }, function (result) {
        $('#list-content2').append(template('list2', {
            list:result.data.list,
            avater:getCookie('photo'),
            name:getCookie('name')
        }));
        hasNextPage = result.data.hasNextPage;
        if (!result.data.hasNextPage) {
            $('#loadmore2').hide();
            $('#nomore2').show();
        } else {
            $('#loadmore2').show();
            $('#nomore2').hide();
        }
        if (callback) callback();
        loading2 = false;
    });
}

//开始创建新的帖子
$('#create_que_botton').click(function(){
    $("#create_que").popup();
});

//确认提交帖子
var isConfirmPosting = false;
$('#confirmPost').click(function(){
    if(!isConfirmPosting)
        createPost();
});

function resetPost(){
    $("#title").val('');
    $("#content").val('');
    $("#showImages").val('');
    $('#ossfile').empty();
}

//创建发布帖子
function createPost() {
    isConfirmPosting = true;
    var tagSelect = $("#secondTags").find('select');
    var tags = '';
    tagSelect && tagSelect.each(function (index, element) {
        tags = tags + $(this).val() + ',';
    });
    if (tags) {
        tags = tags.substring(0, tags.length - 1);
    }
    var title = $("#title").val();
    var content = $("#content").val();
    var showImages = $("#showImages").val();
    if (title == '') {
        $.toast("帖子标题不能为空", "cancel");
        return;
    }
    if (content == '') {
        $.toast("帖子内容不能为空", "cancel");
        return;
    }
    /* if (bondMoney == ''){
        $.toast("保证金不能为空", "cancel");
        return;
    } */
    var params = {};
    params.title = title;
    params.content = content;
    params.image = showImages;
    params.token = getCookie('token');
    $.post('/antique/api/mallPosts/manage/add_posts',params, function (result) {
        if (result.code == 0) {
            $.toast("上传成功!谢谢您的使用!");
            window.setTimeout(function () {
                window.location.reload();
            }, 1000);
        } else {
            $.toast(result.message, "cancel");
        }
        isConfirmPosting = false;
    });
}


